﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;
using QLVatTu.InPhieu;
using System.Globalization;

namespace QLVatTu.NghiepVu
{
    public partial class frmQuanLyNhapTraTuDonViSanXuat : Form
    {
        KetNoiDB cn = new KetNoiDB();

        public frmQuanLyNhapTraTuDonViSanXuat()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            //frmThemPhieuNhapTraDonViSanXuat frm = new frmThemPhieuNhapTraDonViSanXuat();
            frmPhieuTraDonViSanXuat frm = new frmPhieuTraDonViSanXuat();
            frm.ShowDialog();
            load_dtgPhieuNhap();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (TopLevel == false) this.Parent.Parent.Controls.Remove(this.Parent);
            else this.Close();
        }

        private void load_dtgPhieuNhap(string dieuKien = null)
        {
            string sql = "";
            if (dieuKien != null)
            {
                sql = @"SELECT tbl_PhieuNhapKho.SoPhieu, tbl_PhieuNhapKho.NgayNhap, tbl_PhieuNhapKho.MaDoiTac, tbl_DoiTac.TenDoiTac, 
	                           tbl_PhieuNhapKho.MaKho, tbl_Kho.TenKho, tbl_PhieuNhapKho.NguoiNhan, tbl_PhieuNhapKho.DienGiai, 
	                           tbl_PhieuNhapKho.MaLoaiPhieu, tbl_LoaiPhieu.TenLoaiPhieu, tbl_PhieuNhapKho.MaCongTrinhDuAn, 
	                           tbl_CongTrinhDuAn.TenCongTrinhDuAn,tbl_PhieuNhapKho.MaPhieuXuat, XacNhan = case XacNhan when '0' then N'Chưa xác nhận' when '1' then N'Đã xác nhận' end
                        FROM tbl_PhieuNhapKho INNER JOIN 
		                     tbl_DoiTac ON tbl_PhieuNhapKho.MaDoiTac = tbl_DoiTac.MaDoiTac INNER JOIN 
		                     tbl_Kho ON tbl_PhieuNhapKho.MaKho = tbl_Kho.MaKho INNER JOIN 
		                     tbl_LoaiPhieu ON tbl_PhieuNhapKho.MaLoaiPhieu = tbl_LoaiPhieu.MaLoaiPhieu INNER JOIN 
	                         tbl_CongTrinhDuAn ON tbl_PhieuNhapKho.MaCongTrinhDuAn = tbl_CongTrinhDuAn.MaCongTrinhDuAn
                        "
                        + dieuKien;
            }
            else
            {
                sql = @"SELECT tbl_PhieuNhapKho.SoPhieu, tbl_PhieuNhapKho.NgayNhap, tbl_PhieuNhapKho.MaDoiTac, tbl_DoiTac.TenDoiTac, 
	                           tbl_PhieuNhapKho.MaKho, tbl_Kho.TenKho, tbl_PhieuNhapKho.NguoiNhan, tbl_PhieuNhapKho.DienGiai, 
	                           tbl_PhieuNhapKho.MaLoaiPhieu, tbl_LoaiPhieu.TenLoaiPhieu, tbl_PhieuNhapKho.MaCongTrinhDuAn, 
	                           tbl_CongTrinhDuAn.TenCongTrinhDuAn,tbl_PhieuNhapKho.MaPhieuXuat, XacNhan = case XacNhan when '0' then N'Chưa xác nhận' when '1' then N'Đã xác nhận' end
                        FROM tbl_PhieuNhapKho INNER JOIN 
		                     tbl_DoiTac ON tbl_PhieuNhapKho.MaDoiTac = tbl_DoiTac.MaDoiTac INNER JOIN 
		                     tbl_Kho ON tbl_PhieuNhapKho.MaKho = tbl_Kho.MaKho INNER JOIN 
		                     tbl_LoaiPhieu ON tbl_PhieuNhapKho.MaLoaiPhieu = tbl_LoaiPhieu.MaLoaiPhieu INNER JOIN 
	                         tbl_CongTrinhDuAn ON tbl_PhieuNhapKho.MaCongTrinhDuAn = tbl_CongTrinhDuAn.MaCongTrinhDuAn
                        where tbl_PhieuNhapKho.MaLoaiPhieu='LP002'";
            }
           DataTable tbl = cn.getTable(sql);
           dtgPhieuNhap.DataSource = tbl;
        }

        private void loadData()
        {
            cbbKhoNhap.DataSource = cn.getTable("select * from tbl_Kho");
            cbbKhoNhap.DisplayMember = "TenKho";
            cbbKhoNhap.ValueMember = "MaKho";

            cbbDonViSanXuat.DataSource = cn.getTable("select * from tbl_DoiTac where DonViSanXuat=1");
            cbbDonViSanXuat.DisplayMember = "TenDoiTac";
            cbbDonViSanXuat.ValueMember = "MaDoiTac";

            cbbCongTrinhDuAn.DataSource = cn.getTable("select * from tbl_CongTrinhDuAn");
            cbbCongTrinhDuAn.DisplayMember = "TenCongTrinhDuAn";
            cbbCongTrinhDuAn.ValueMember = "MaCongTrinhDuAn";

            load_dtgPhieuNhap();
        }

        private void frmQuanLyNhapTraTuDonViSanXuat_Load(object sender, EventArgs e)
        {
            if (cn.KetNoi())
            {
                loadData();
            }
        }

        private void dtgPhieuNhap_SelectionChanged(object sender, EventArgs e)
        {
            if (dtgPhieuNhap.SelectedRows.Count > 0 && dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value!=null)
            {
                string sql = @"SELECT row_number() over(order by tbl_ChiTietPhieuNhap.MaChiTietPhieuNhap) as STT, tbl_ChiTietPhieuNhap.MaVatTu, 
                                    tbl_VatTu.TenVatTu, tbl_ChiTietPhieuNhap.MaDonViTinh, tbl_DonViTinh.TenDonViTinh, tbl_ChiTietPhieuNhap.SoLuong, 
                                    tbl_ChiTietPhieuNhap.SoLuongPhePham 
                             FROM tbl_ChiTietPhieuNhap INNER JOIN 
                                    tbl_DonViTinh ON tbl_ChiTietPhieuNhap.MaDonViTinh = tbl_DonViTinh.MaDonViTinh INNER JOIN 
                                    tbl_VatTu ON tbl_ChiTietPhieuNhap.MaVatTu = tbl_VatTu.MaVatTu 
                             where tbl_ChiTietPhieuNhap.SoPhieu='" + dtgPhieuNhap.SelectedRows[0].Cells["colSoPhieu"].Value.ToString() + "'";
                DataTable tbl = cn.getTable(sql);
                dtgChiTietPhieuNhapTra.DataSource = tbl;

                sql = "SELECT [XacNhan] FROM [tbl_PhieuNhapKho] WHERE [SoPhieu] = '" + dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                bool _XacNhan = bool.Parse(cn.LayDuLieu(sql));
                btnXacNhan.Enabled = !_XacNhan;
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (dtgPhieuNhap.SelectedRows.Count == 0)
            {
                MessageBox.Show("Chọn phiếu cần sửa.","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            else
            {
                string sql = "SELECT [XacNhan] FROM [tbl_PhieuNhapKho] WHERE [SoPhieu] = '" + dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                bool _XacNhan = bool.Parse(cn.LayDuLieu(sql));
                if (_XacNhan == true)
                {
                    MessageBox.Show("Bạn không thể sửa phiếu đã xác nhận", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                frmPhieuTraDonViSanXuat frm = new frmPhieuTraDonViSanXuat();
                frm.MaPhieuNhapTra = dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                frm.ShowDialog();
                load_dtgPhieuNhap();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dtgPhieuNhap.RowCount == 0) return;

            string sql = "SELECT [XacNhan] FROM [tbl_PhieuNhapKho] WHERE [SoPhieu] = '" + dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
            bool _XacNhan = bool.Parse(cn.LayDuLieu(sql));
            if (_XacNhan == true)
            {
                MessageBox.Show("Bạn không thể xóa phiếu đã xác nhận.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else
            {
                DialogResult msg = MessageBox.Show("Bạn chắc chắn muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (msg == DialogResult.Yes)
                {
                    cn.Delete_Table("tbl_ChiTietPhieuNhap", "SoPhieu='" + dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                    cn.Delete_Table("tbl_PhieuNhapKho", "SoPhieu='" + dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                }
            }
            load_dtgPhieuNhap();
        }

        private void ckbMaPhieu_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbMaPhieu.Checked == true)
            {
                txtSoPhieu.Text = "";
                txtSoPhieu.Enabled = true;
            }
            else
            {
                txtSoPhieu.Text = "Tìm theo mã phiếu";
                txtSoPhieu.Enabled = false;
            }

        }

        private void ckbKhoNhap_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbKhoNhap.Checked == true)
            {
                cbbKhoNhap.Enabled = true;
            }
            else
            {
                cbbKhoNhap.Enabled = false;
            }
        }

        private void ckbDonViSanXuat_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbDonViSanXuat.Checked == true)
            {
                cbbDonViSanXuat.Enabled = true;
            }
            else
            {
                cbbDonViSanXuat.Enabled = false;
            }
        }

        private void ckbNguoiNhan_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbNguoiNhan.Checked == true)
            {
                txtNguoiNhan.Text = "";
                txtNguoiNhan.Enabled = true;
            }
            else
            {
                txtNguoiNhan.Text = "Tìm theo người lập phiếu";
                txtNguoiNhan.Enabled = false;
            }
        }

        private void ckbCongTrinhDuAn_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbCongTrinhDuAn.Checked == true)
            {
                cbbCongTrinhDuAn.Enabled = true;
            }
            else
            {
                cbbCongTrinhDuAn.Enabled = false;
            }
        }

        private string XacDinhDieuKienLoc()
        {
            string dieuKienLoc = "where 1=1 and tbl_PhieuNhapKho.MaLoaiPhieu='LP002'";

            if (ckbMaPhieu.Checked == true && txtSoPhieu.Text.Trim() != "")
            {
                dieuKienLoc += "and tbl_PhieuNhapKho.SoPhieu like '%" + txtSoPhieu.Text.Trim() + "%'";
            }

            if (ckbKhoNhap.Checked == true && cbbKhoNhap.Items.Count > 0)
            {
                dieuKienLoc += "and tbl_PhieuNhapKho.MaKho='" + cbbKhoNhap.SelectedValue.ToString() + "'";
            }

            if (ckbDonViSanXuat.Checked == true && cbbDonViSanXuat.Items.Count > 0)
            {
                dieuKienLoc += "and tbl_PhieuNhapKho.MaDoiTac='" + cbbDonViSanXuat.SelectedValue.ToString() + "'";
            }

            if (ckbNguoiNhan.Checked == true && txtNguoiNhan.Text.Trim() != "")
            {
                dieuKienLoc += "and tbl_PhieuNhapKho.NguoiNhan like '%" + txtNguoiNhan.Text.Trim() + "%'";
            }

            if (ckbCongTrinhDuAn.Checked == true && cbbCongTrinhDuAn.Items.Count > 0)
            {
                dieuKienLoc += "and tbl_PhieuNhapKho.MaCongTrinhDuAn='" + cbbCongTrinhDuAn.SelectedValue.ToString() + "'";
            }

            if (ckbNgayNhap.Checked == true)
            {
                dieuKienLoc += "and tbl_PhieuNhapKho.NgayNhap between '" + dtipTuNgay.Value.ToString() + "' and '" + dtipDenNgay.Value.ToString() + "'";
            }

            return dieuKienLoc;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            load_dtgPhieuNhap(XacDinhDieuKienLoc());
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            frmInPhieuNhapTra frm = new frmInPhieuNhapTra();
            if (dtgPhieuNhap.Rows.Count > 0 && dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value != null)
            {
                string SoPhieu = dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                List<PhieuNhapTra> list = new List<PhieuNhapTra>();
                string Ngay = "";
                string Thang = "";
                string Nam = "";
                string DoiTac = "";
                string DiaChi = "";
                string TenKho = "";
                string NguoiNhan = "";
                string DienGiai = "";
                string CongTrinhDuAn = "";
                string HangMuc = "";

                string sql = "";
                sql = @"SELECT tbl_PhieuNhapKho.SoPhieu, tbl_PhieuNhapKho.NgayNhap, tbl_PhieuNhapKho.MaDoiTac, tbl_DoiTac.TenDoiTac, tbl_DoiTac.DiaChi,
	                           tbl_PhieuNhapKho.MaKho, tbl_Kho.TenKho, tbl_PhieuNhapKho.NguoiNhan, tbl_PhieuNhapKho.DienGiai, 
	                           tbl_PhieuNhapKho.MaLoaiPhieu, tbl_LoaiPhieu.TenLoaiPhieu, tbl_PhieuNhapKho.MaCongTrinhDuAn, 
	                           tbl_CongTrinhDuAn.TenCongTrinhDuAn 
                        FROM tbl_PhieuNhapKho INNER JOIN 
		                     tbl_DoiTac ON tbl_PhieuNhapKho.MaDoiTac = tbl_DoiTac.MaDoiTac INNER JOIN 
		                     tbl_Kho ON tbl_PhieuNhapKho.MaKho = tbl_Kho.MaKho INNER JOIN 
		                     tbl_LoaiPhieu ON tbl_PhieuNhapKho.MaLoaiPhieu = tbl_LoaiPhieu.MaLoaiPhieu INNER JOIN 
	                         tbl_CongTrinhDuAn ON tbl_PhieuNhapKho.MaCongTrinhDuAn = tbl_CongTrinhDuAn.MaCongTrinhDuAn
	                    WHERE tbl_PhieuNhapKho.MaLoaiPhieu='LP002'";
                DataTable tbl = cn.getTable(sql);
                if (tbl.Rows.Count > 0)
                {
                    string NgayNhap=tbl.Rows[0]["NgayNhap"].ToString();
                    DateTime dtNgayNhap = DateTime.Parse(NgayNhap);
                    Ngay = dtNgayNhap.Day.ToString();
                    Thang = dtNgayNhap.Day.ToString();
                    Nam = dtNgayNhap.Year.ToString();
                    DoiTac = tbl.Rows[0]["TenDoiTac"].ToString();
                    DiaChi=tbl.Rows[0]["DiaChi"].ToString();
                    TenKho=tbl.Rows[0]["TenKho"].ToString();
                    NguoiNhan=tbl.Rows[0]["NguoiNhan"].ToString();
                    DienGiai = tbl.Rows[0]["DienGiai"].ToString();
                    CongTrinhDuAn=tbl.Rows[0]["TenCongTrinhDuAn"].ToString();
                    HangMuc=tbl.Rows[0]["TenLoaiPhieu"].ToString();
                }

                sql = @"SELECT row_number() over(order by tbl_ChiTietPhieuNhap.MaChiTietPhieuNhap) as STT, tbl_ChiTietPhieuNhap.MaVatTu, 
		                        tbl_VatTu.TenVatTu, tbl_ChiTietPhieuNhap.MaDonViTinh, tbl_DonViTinh.TenDonViTinh, tbl_ChiTietPhieuNhap.SoLuong, 
		                        tbl_ChiTietPhieuNhap.SoLuongPhePham 
                        FROM tbl_ChiTietPhieuNhap INNER JOIN 
		                        tbl_DonViTinh ON tbl_ChiTietPhieuNhap.MaDonViTinh = tbl_DonViTinh.MaDonViTinh INNER JOIN 
		                        tbl_VatTu ON tbl_ChiTietPhieuNhap.MaVatTu = tbl_VatTu.MaVatTu 
                        where tbl_ChiTietPhieuNhap.SoPhieu='"+SoPhieu+"'";
                DataTable dataChiTiet = cn.getTable(sql);
                if (dataChiTiet.Rows.Count > 0)
                {
                    int stt = 0;
                    foreach (DataRow dr in dataChiTiet.Rows)
                    {
                        stt++;
                        PhieuNhapTra pnt = new PhieuNhapTra();
                        pnt.SoPhieu = SoPhieu;
                        pnt.Ngay = Ngay;
                        pnt.Thang = Thang;
                        pnt.Nam = Nam;
                        pnt.DoiTac = DoiTac;
                        pnt.DiaChi = DiaChi;
                        pnt.TenKho = TenKho;
                        pnt.NguoiNhan = NguoiNhan;
                        pnt.DienGiai = DienGiai;
                        pnt.CongTrinhDuAn = CongTrinhDuAn;

                        pnt.STT = stt.ToString();
                        pnt.MaVT=dr["MaVatTu"].ToString();
                        pnt.TenVatTu=dr["TenVatTu"].ToString();
                        pnt.DonViTinh=dr["TenDonViTinh"].ToString();
                        pnt.HangMuc=HangMuc;
                        pnt.SoLuong=dr["SoLuong"].ToString();
                        pnt.SLPhePham=dr["SoLuongPhePham"].ToString();
                        list.Add(pnt);
                    }
                }
                frm.phieuNhapTraBindingSource.DataSource = list;
                frm.ShowDialog();
            }
        }

        private void ckbNgayNhap_CheckedChanged(object sender, EventArgs e)
        {
            dtipTuNgay.Enabled = ckbNgayNhap.Checked;
            dtipDenNgay.Enabled = ckbNgayNhap.Checked;
        }

        private void radLoad_CheckedChanged(object sender, EventArgs e)
        {
            if (radLoad.Checked == true)
            {
                radLoad.Checked = false;
                load_dtgPhieuNhap();
                ckbNgayNhap.Checked = false;
                ckbKhoNhap.Checked = false;
                ckbMaPhieu.Checked = false;
                ckbNguoiNhan.Checked = false;
                ckbCongTrinhDuAn.Checked = false;
                ckbDonViSanXuat.Checked = false;
            }
        }

        private void btnXacNhan_Click(object sender, EventArgs e)
        {
            if (dtgPhieuNhap.SelectedRows.Count > 0 && dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value != null)
            {
                
                    foreach (DataGridViewRow dgvr in dtgChiTietPhieuNhapTra.Rows)
                    {
                        if (dgvr.IsNewRow) continue;
                        string sql = "SELECT MaKho FROM dbo.tbl_PhieuNhapKho WHERE SoPhieu = '" + dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                        string MaKho = cn.LayDuLieu(sql);
                        string MaVatTu = dgvr.Cells["colMaVatTu"].Value.ToString();
                        string SoPhieu = dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                        string NgayLapPhieu = dtgPhieuNhap.CurrentRow.Cells["colNgayNhap"].Value.ToString();

                        float SoLuongDaCap = 0;
                        sql = @"SELECT     SUM(dbo.tbl_ChiTietPhieuXuat.SoLuong) AS SoLuongDaCap
                            FROM         dbo.tbl_ChiTietPhieuXuat INNER JOIN
                                                  dbo.tbl_PhieuXuatKho ON dbo.tbl_ChiTietPhieuXuat.SoPhieu = dbo.tbl_PhieuXuatKho.SoPhieu
                            WHERE     dbo.tbl_PhieuXuatKho.SoPhieu = '" +dtgPhieuNhap.CurrentRow.Cells["MaPhieuXuat"].Value.ToString() + "' AND dbo.tbl_ChiTietPhieuXuat.MaVatTu = '" + MaVatTu + @"' and XacNhan=1
                            GROUP BY dbo.tbl_ChiTietPhieuXuat.MaVatTu";
                        DataTable dtSoLuongDaCap = cn.getTable(sql);
                        if (dtSoLuongDaCap.Rows.Count > 0) SoLuongDaCap = float.Parse(dtSoLuongDaCap.Rows[0]["SoLuongDaCap"].ToString());

                        float SoLuongDaNhap = 0;
                        sql = @"SELECT     SUM(dbo.tbl_ChiTietPhieuNhap.SoLuong) AS SoLuongDaNhap
                            FROM         dbo.tbl_ChiTietPhieuNhap INNER JOIN
                                                  dbo.tbl_PhieuNhapKho ON dbo.tbl_ChiTietPhieuNhap.SoPhieu = dbo.tbl_PhieuNhapKho.SoPhieu
                            WHERE     dbo.tbl_PhieuNhapKho.MaPhieuXuat = '" + dtgPhieuNhap.CurrentRow.Cells["MaPhieuXuat"].Value.ToString() + "' and tbl_PhieuNhapKho.XacNhan=1 AND dbo.tbl_ChiTietPhieuNhap.MaVatTu = '" + MaVatTu + @"'
                            GROUP BY dbo.tbl_ChiTietPhieuNhap.MaVatTu";
                        DataTable dtSoLuong = cn.getTable(sql);
                        if (dtSoLuong.Rows.Count > 0) SoLuongDaNhap = float.Parse(dtSoLuong.Rows[0]["SoLuongDaNhap"].ToString());

                        string strSoLuong = "";
                        double SoLuong = 0;

                        string strSoLuongPhePham = "";
                        double SoLuongPhePham = 0;

                        if (dgvr.Cells["colSoLuong"].Value.ToString().Contains(','))
                        {
                            strSoLuong = dgvr.Cells["colSoLuong"].Value.ToString().Replace(',', '.');
                        }
                        else
                        {
                            strSoLuong = dgvr.Cells["colSoLuong"].Value.ToString();
                        }
                        double.TryParse(strSoLuong, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out SoLuong);

                        //Kiểm tra số lượng phế phẩm
                        if (dgvr.Cells["colSoLuongPhePham"].Value.ToString().Contains(','))
                        {
                            strSoLuongPhePham = dgvr.Cells["colSoLuongPhePham"].Value.ToString().Replace(',', '.');
                        }
                        else
                        {
                            strSoLuongPhePham = dgvr.Cells["colSoLuongPhePham"].Value.ToString();
                        }
                        double.TryParse(strSoLuongPhePham, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out SoLuongPhePham);

                        if (SoLuong > SoLuongDaCap - SoLuongDaNhap || SoLuongPhePham > SoLuongDaCap - SoLuongDaNhap)
                        {
                            MessageBox.Show("Không thể xác nhận phiếu","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
                            return;
                        }
                        else
                        {
                            DialogResult msg = MessageBox.Show("Sau khi xác nhận bạn không thể sửa và xóa phiếu!", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                            if (msg == DialogResult.Yes)
                            {
                                cn.Update_Table("tbl_PhieuNhapKho", "XacNhan=1", "SoPhieu='" + dtgPhieuNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                                cn.Insert_Table("tbl_LuuKho", "SoPhieu,MaKho,MaVatTu,SoLuong,SoLuongPhePham,NgayCapNhat", "'" + SoPhieu + "','" + MaKho + "','" + MaVatTu + "','" + dgvr.Cells["colSoLuong"].Value.ToString() + "','" + dgvr.Cells["colSoLuongPhePham"].Value.ToString() + "','" + NgayLapPhieu + "'");
                            }
                    }
                    load_dtgPhieuNhap();
                }
            }
        }
    }
}
